package com.wwp.tbshop.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.AddressList;
import com.wwp.tbshop.presenter.AddressChangePresenter;
import com.wwp.tbshop.presenter.AddressPresenter;
import com.wwp.tbshop.util.SPUtils;
import com.wwp.tbshop.view.adapter.AddressRVAdapter;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.http.Header;

/**
 * Created by Xiaokun on 2017/6/15.
 */

public class AddressManageActivity extends BaseActivity {

    public static final int REQUEST_CODE = 10;
    @Bind(R.id.rv_address_list)
    RecyclerView mAddressListRV;
    @Bind(R.id.tv_add_address)
    TextView tvAddAddress;
    @Bind(R.id.iv_manage_back)
    ImageView ivManageBack;
    private AddressRVAdapter mRvAdapter;
    private AddressPresenter mAddressPresenter;

    private int userId = 182843;
    private List<AddressList.AddressListBean> mAddressListBean;


    @Override
    public int getLayoutId() {
        return R.layout.activity_address_manage;
    }

    public void init() {
        String userid = SPUtils.getString(this, SPUtils.USERID, null);
        if (userid != null) {
            userId = Integer.parseInt(userid);
        }
        mAddressPresenter = new AddressPresenter(this);
        mAddressPresenter.getAddressList(userId);
        mAddressListRV.setLayoutManager(new LinearLayoutManager(this));
        mRvAdapter = new AddressRVAdapter(AddressManageActivity.this);
        mAddressListRV.setAdapter(mRvAdapter);
        mRvAdapter.setUserid(userId);
    }

    @OnClick({R.id.tv_add_address, R.id.iv_manage_back})
    public void onAddClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add_address:
                Intent intent = new Intent(this, AddOrEditAddressActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.iv_manage_back:
                finish();
                break;
        }
    }

    private static final String TAG = "AddressManageActivity";


    public void onSuccess(AddressList addressList) {
        mAddressListBean = addressList.getAddressList();
        mRvAdapter.setDatas(addressList);
        mRvAdapter.notifyDataSetChanged();
        Log.e(TAG, "onSuccess:" + "更新");
    }

    public void updateSuccess() {
        //mRvAdapter.notifyDataSetChanged();
        Log.e(TAG, "updateSuccess:" + "更新" + userId);
        mAddressPresenter.getAddressList(userId);
        mRvAdapter.onSetDefaultResult();

    }

    @Override
    protected void onResume() {
        Log.e(TAG, "onPostResume:" + "焦点");
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            AddressList.AddressListBean addressbean = (AddressList.AddressListBean) data.getSerializableExtra("addressbean_updata");
            AddressChangePresenter presenter = new AddressChangePresenter(this);
            Log.e(TAG, "onActivityResult: " + userId);
            if (addressbean.getId() > 0) {
                presenter.saveAddress(addressbean.getId() + "", addressbean.getName(), addressbean.getPhoneNumber(), addressbean.getProvince(), addressbean.getCity(), addressbean.getAddressArea(), addressbean.getAddressDetail(), addressbean.getZipCode(), addressbean.getIsDefault(), userId);
                presenter.setAddressDefault(addressbean.getId(), userId);
            } else {
                presenter.saveAddress("", addressbean.getName(), addressbean.getPhoneNumber(), addressbean.getProvince(), addressbean.getCity(), addressbean.getAddressArea(), addressbean.getAddressDetail(), addressbean.getZipCode(), addressbean.getIsDefault(), userId);
            }
        }
    }
}
